package org.yx.service.sqcm.save_defective_code;

import org.yx.exception.BizException;
import org.yx.models.SqcmDftCod;
import org.yx.utils.BaseService;

import com.xiaoleilu.hutool.util.RandomUtil;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 保存（新增或修改）不良代码定义
 * 
 * @author zxw
 * @since 2019/05/30
 **/
public class SqcmSaveDefectiveCodeService extends BaseService {
	public SqcmSaveDefectiveCodeOut saveDefectiveCode(SqcmSaveDefectiveCodeIn in) throws Exception {
		if (StrUtil.isNotBlank(in.getId())) {
			SqcmDftCod dftCode = selectOneLocked(SqcmDftCod.class, in.getId());
			if (dftCode == null || dftCode.getStatus() == 'D') {
				throw new BizException("SQCM-0003", "不良代码数据不存在或已删除。");
			}
			
			copyProperties(in, dftCode);
			
			update(dftCode);
		}
		else{
			SqcmDftCod dftcode = new SqcmDftCod();
			copyProperties(in, dftcode);
			dftcode.setId(RandomUtil.simpleUUID());
			dftcode.setStatus('N');
			dftcode.setOrgCode(in.getUserOrgCode());
			
			insert(dftcode);
		}

		SqcmSaveDefectiveCodeOut out = new SqcmSaveDefectiveCodeOut();

		return out;
	}
}